package com.tongbo.mobilesafe.ui;

import com.tongbo.mobilesafe.R;
import com.tongbo.mobilesafe.util.MD5Encoder;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class LostProtectedActivity extends Activity implements OnClickListener{
	private static final String TAG = "LostProtectedActivity";
	private SharedPreferences sp;
	private Dialog dialog;
	private EditText et_pwd;
	private EditText et_pwd_confirm;
	private TextView tv_lost_protected_number;
	private CheckBox cb_isprotecting;
	private TextView tv_reentry_setup_guide;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sp = this.getSharedPreferences("config", Context.MODE_PRIVATE);
		
		//判断用户是否已经设置了密码
		Log.i(TAG, "-------------LostProtectedActivity--------------");
		if(isPWDSetup()){
			Log.i(TAG, "正常设置了密码，正常登陆的对话框");
			showNormalEntryDialog();
		}else{
			Log.i(TAG,"没有设置密码,显示第一次对话框");
			showFirstEntryDialog();
		}
	}
	/**
	 * 正常登陆的对话框
	 */
	private void showNormalEntryDialog() {
		dialog = new Dialog(this, R.style.MyDialog);
		//dialog.setContentView(R.layout.first_entry_dialog);
		View view = View.inflate(this, R.layout.normal_entry_dialog, null);
		et_pwd = (EditText) view.findViewById(R.id.et_normal_entry_pwd);
		
		Button bt_normal_ok = (Button) view.findViewById(R.id.bt_normal_dialog_ok);
		Button bt_normal_cancel = (Button) view.findViewById(R.id.bt_normal_dialog_cancel);
		bt_normal_ok.setOnClickListener(this);
		bt_normal_cancel.setOnClickListener(this);
		dialog.setContentView(view);
		dialog.show();
		
	}
	/**
	 * 第一次进入手机防盗时的对话框
	 */
	private void showFirstEntryDialog() {
		dialog = new Dialog(this, R.style.MyDialog);
		//dialog.setContentView(R.layout.first_entry_dialog);
		View view = View.inflate(this, R.layout.first_entry_dialog, null);
		et_pwd = (EditText) view.findViewById(R.id.et_first_entry_pwd);
		et_pwd_confirm = (EditText) view.findViewById(R.id.et_first_entry_pwd_confirm);
		
		Button bt_ok = (Button) view.findViewById(R.id.bt_first_dialog_ok);
		Button bt_cancel = (Button) view.findViewById(R.id.bt_first_dialog_cancel);
		bt_ok.setOnClickListener(this);
		bt_cancel.setOnClickListener(this);
		dialog.setContentView(view);
		dialog.show();
	}
	/**
	 * 检查用户是否已经设置了密码
	 * @return
	 */
	private boolean isPWDSetup(){
		String password = sp.getString("password", null);
		if(null==password){
			return false;
		}else if("".equals(password)){
			return false;
		}else{
			return true;
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.bt_first_dialog_ok:
				String pwd = et_pwd.getText().toString().trim();
				String pwd_confirm = et_pwd_confirm.getText().toString().trim();
				if("".equals(pwd)){
					Toast.makeText(getApplicationContext(), "密码不能为空", 0).show();
					return;
				}else if("".equals(pwd_confirm)){
					Toast.makeText(getApplicationContext(), "请输入确认密码", 0).show();
					return;
				}else{
					 if(pwd.equals(pwd_confirm)){
						 Editor editor = sp.edit();
						 editor.putString("password", MD5Encoder.encode(pwd));
						 editor.commit();
					 }else{
						 Toast.makeText(getApplicationContext(), "两次密码输入不一致", 0).show();
						 return;
					 }
				}
				dialog.dismiss();
				break;
	
			case R.id.bt_first_dialog_cancel:
				dialog.dismiss();
				finish();
				break;
			case R.id.bt_normal_dialog_ok:
				String password = et_pwd.getText().toString().trim();
				if("".equals(password)){
					Toast.makeText(getApplicationContext(), "请输入密码", 0).show();
					return;
				}else{ 
					String realpwd = sp.getString("password", "");
					if (realpwd.equals(MD5Encoder.encode(password))){
						if(isSetup()){
							Log.i(TAG,"加载手机防盗主界面");
							setContentView(R.layout.lost_protected);
							tv_lost_protected_number = (TextView) this.findViewById(R.id.tv_lost_protected_number);
							tv_reentry_setup_guide = (TextView) this.findViewById(R.id.tv_reentry_setup_guide);
							cb_isprotecting = (CheckBox) this.findViewById(R.id.cb_isprotecting);
							//初始化控件
							String number = sp.getString("safenumber", "");
							tv_lost_protected_number.setText("手机安全号码为:"+number);
							//重新进入设置向导的点击事件
							tv_reentry_setup_guide.setOnClickListener(this);
							
							//初始化checkbox状态
							boolean isprotecting = sp.getBoolean("isprotecting", false);
							if(isprotecting){
								cb_isprotecting.setText("手机防盗保护中");
								cb_isprotecting.setChecked(true);
							}
							cb_isprotecting.setOnCheckedChangeListener(new OnCheckedChangeListener() {
								
								@Override
								public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
									if(isChecked){
										cb_isprotecting.setText("手机防盗保护中");
										cb_isprotecting.setChecked(true);
										Editor editor = sp.edit();
										editor.putBoolean("isprotecting", true);
										editor.commit();
									}else{
										cb_isprotecting.setText("手机没有开启防盗能保护");
										cb_isprotecting.setChecked(false);
										Editor editor = sp.edit();
										editor.putBoolean("isprotecting", false);
										editor.commit();
									}
								}
							});
						}else{
							Log.i(TAG,"激活手机防盗向导");
							finish();
							Intent intent = new Intent(LostProtectedActivity.this, SetupGuide1Activity.class);
							startActivity(intent);
						}
					}else{
						Toast.makeText(getApplicationContext(), "密码错误", 0).show();
						return;
					}
				}
				dialog.dismiss();
				break;
			case R.id.bt_normal_dialog_cancel:
				dialog.dismiss();
				finish();
				break;
			case R.id.tv_reentry_setup_guide:
				finish();
				Intent intent = new Intent(LostProtectedActivity.this, SetupGuide1Activity.class);
				startActivity(intent);
				break;
		}
	}
	/**
	 * 判断用户是否设置过向导
	 * @return
	 */
	private boolean isSetup(){
		return sp.getBoolean("issetupalready", false);
	}
}